package com.ruoyi.laiqianba.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.laiqianba.service.IHandleCacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.db.mapper.MemberSourceDictInfoMapper;
import com.ruoyi.db.domain.MemberSourceDictInfo;
import com.ruoyi.laiqianba.service.IMemberSourceDictInfoService;

import javax.annotation.Resource;

/**
 * 用户资质字典管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-08-13
 */
@Service
@DataSource(DataSourceType.SLAVE)
public class MemberSourceDictInfoServiceImpl implements IMemberSourceDictInfoService 
{
    @Resource
    private MemberSourceDictInfoMapper memberSourceDictInfoMapper;
    @Autowired
    IHandleCacheService handleCacheService;

    /**
     * 查询用户资质字典管理
     * 
     * @param id 用户资质字典管理主键
     * @return 用户资质字典管理
     */
    @Override
    public MemberSourceDictInfo selectMemberSourceDictInfoById(Long id)
    {
        return memberSourceDictInfoMapper.selectMemberSourceDictInfoById(id);
    }

    @Override
    public String selectNameByValue(String value) {
        return memberSourceDictInfoMapper.selectNameByValue(value);
    }

    /**
     * 查询用户资质字典管理列表
     * 
     * @param memberSourceDictInfo 用户资质字典管理
     * @return 用户资质字典管理
     */
    @Override
    public List<MemberSourceDictInfo> selectMemberSourceDictInfoList(MemberSourceDictInfo memberSourceDictInfo)
    {
        return memberSourceDictInfoMapper.selectMemberSourceDictInfoList(memberSourceDictInfo);
    }
    @Override
    public List<Map<String, Object>> selectMemberSourceDictInfoMapList(MemberSourceDictInfo memberSourceDictInfo)
    {
        return memberSourceDictInfoMapper.selectMemberSourceDictInfoMapList(memberSourceDictInfo);
    }
   @Override
    public List<Map<String, Object>> selectMemberSourceDictInfoTaskMapList(MemberSourceDictInfo memberSourceDictInfo)
    {
        return memberSourceDictInfoMapper.selectMemberSourceDictInfoTaskMapList(memberSourceDictInfo);
    }

    /**
     * 新增用户资质字典管理
     * 
     * @param memberSourceDictInfo 用户资质字典管理
     * @return 结果
     */
    @Override
    public int insertMemberSourceDictInfo(MemberSourceDictInfo memberSourceDictInfo)
    {
        if(memberSourceDictInfo.getParentId()!=0){
            memberSourceDictInfo.setType(2L);
        }
        memberSourceDictInfo.setCreateTime(DateUtils.getNowDate());

        int i = memberSourceDictInfoMapper.insertMemberSourceDictInfo(memberSourceDictInfo);
        if (i>0){
            // 删除父级 的redis 缓存
            handleCacheService.clearSourceDictByParentId(String.valueOf(memberSourceDictInfo.getParentId()));
        }
        return i;
    }

    /**
     * 修改用户资质字典管理
     * 
     * @param memberSourceDictInfo 用户资质字典管理
     * @return 结果
     */
    @Override
    public int updateMemberSourceDictInfo(MemberSourceDictInfo memberSourceDictInfo)
    {
        memberSourceDictInfo.setUpdateTime(DateUtils.getNowDate());
        if(memberSourceDictInfo.getParentId()!=0){
            memberSourceDictInfo.setType(2L);
        }

        int i = memberSourceDictInfoMapper.updateMemberSourceDictInfo(memberSourceDictInfo);
        if (i>0){
            // 删除父级 的redis 缓存
            handleCacheService.clearSourceDictByParentId(String.valueOf(memberSourceDictInfo.getParentId()));
        }

        return i;
    }

    /**
     * 批量删除用户资质字典管理
     * 
     * @param ids 需要删除的用户资质字典管理主键
     * @return 结果
     */
    @Override
    public int deleteMemberSourceDictInfoByIds(Long[] ids)
    {
        return memberSourceDictInfoMapper.deleteMemberSourceDictInfoByIds(ids);
    }

    /**
     * 删除用户资质字典管理信息
     * 
     * @param id 用户资质字典管理主键
     * @return 结果
     */
    @Override
    public int deleteMemberSourceDictInfoById(Long id)
    {
        return memberSourceDictInfoMapper.deleteMemberSourceDictInfoById(id);
    }

    @Override
    public Map<String, String> selectSourceDictMap() {

        Map<String, String> sourceDictMap = new HashMap<>();
        List<MemberSourceDictInfo> memberSourceDictInfos = this.selectMemberSourceDictInfoList(new MemberSourceDictInfo());
        for (MemberSourceDictInfo memberSourceDictInfo : memberSourceDictInfos) {
            sourceDictMap.put(memberSourceDictInfo.getValue(), memberSourceDictInfo.getName());
        }

        return sourceDictMap;
    }

    @Override
    public List<Map<String, String>> getOrFilterAll() {
        return memberSourceDictInfoMapper.getOrFilterAll();
    }
}
